package com.yinhe.music.yhmusic.widget;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;

import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.constants.Actions;
import com.yinhe.music.yhmusic.constants.Extras;
import com.yinhe.music.yhmusic.model.Music;
import com.yinhe.music.yhmusic.music.PlayingActivity;
import com.yinhe.music.yhmusic.utils.FileUtils;

public class PlayingNotification implements Actions {
    private boolean isPlaying;
    private Context mContext;
    private Music music;
    private NotificationCompat.Builder builder;

    public PlayingNotification(boolean isPlaying, Context mContext, Music music) {
        this.isPlaying = isPlaying;
        this.mContext = mContext;
        this.music = music;
        init();
    }

    public Notification getNotification() {
        return builder.build();
    }

    private void init(){
        String title = music.getTitle();
        String subtitle = FileUtils.getArtistAndAlbum(music.getArtist(), music.getAlbumName());
        RemoteViews mRemoteViews = new RemoteViews(mContext.getPackageName(), R.layout.notification_layout);

        mRemoteViews.setImageViewResource(R.id.image, R.mipmap.ic_logo);

        mRemoteViews.setTextViewText(R.id.name, title);
        mRemoteViews.setTextViewText(R.id.singer, subtitle);

        Intent pauseIntent = new Intent(ACTION_MEDIA_PLAY_PAUSE);
        PendingIntent pausePIntent = PendingIntent.getBroadcast(mContext, 0, pauseIntent, 0);
        mRemoteViews.setImageViewResource(R.id.play, isPlaying ? R.drawable.ic_notice_stop : R.drawable.ic_notice_play);
        mRemoteViews.setOnClickPendingIntent(R.id.play, pausePIntent);

        Intent nextIntent = new Intent(ACTION_MEDIA_NEXT);
        PendingIntent nextPIntent = PendingIntent.getBroadcast(mContext, 0, nextIntent, 0);
        mRemoteViews.setOnClickPendingIntent(R.id.next, nextPIntent);

        Intent prevIntent = new Intent(ACTION_MEDIA_NEXT);
        PendingIntent prevPIntent = PendingIntent.getBroadcast(mContext, 0, prevIntent, 0);
        mRemoteViews.setOnClickPendingIntent(R.id.before, prevPIntent);

        Intent intent = new Intent(mContext, PlayingActivity.class);
        intent.putExtra(Extras.FROM_NOTIFICATION, true);
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);

        builder = new NotificationCompat.Builder(mContext,"default");
        builder.setContentIntent(pendingIntent);
        builder.setContent(mRemoteViews);
        builder.setSmallIcon(R.drawable.ic_notification);
        builder.setCustomBigContentView(mRemoteViews);
        // 【适配Android8.0】设置Notification的Channel_ID,否则不能正常显示
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId("notification_id");
        }
    }


}
